//
//  HeadlineDetailsView.m
//  Village
//
//  Created by 程虎 on 2017/9/8.
//  Copyright © 2017年 程虎. All rights reserved.
//

#import "HeadlineDetailsView.h"
#import "PhotosContainerView.h"

@interface HeadlineDetailsView()

@property (nonatomic, strong) UIImageView *newsType;

@property (nonatomic, strong) UILabel *timeLab;
@property (nonatomic, strong) UILabel *titleLab;
@property (nonatomic, strong) UILabel *addressLab;
@property (nonatomic, strong) UILabel *contentLab;
@property (nonatomic, strong) PhotosContainerView *photosContainer;
@end

@implementation HeadlineDetailsView

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self set_UI];
    }
    return self;
}

- (void)setModel:(VillageModel *)model
{
    _model = model;
    [self.iconImg sd_setImageWithURL:[NSURL URLWithString:[Tools resolutionHeadImage:_model.headimg]] placeholderImage:[UIImage imageNamed:@"user_icon"]];
    self.nameLab.text = _model.nickname.length > 0 ? _model.nickname : _model.publishuser;
    NSString *times = _model.publishtime.length > 0 ? _model.publishtime : @"";
    if (times.length > 0){
        times = [times substringToIndex:19];//截取掉下标7之后的字符串
    }
    self.timeLab.text = [NSString stringWithFormat:@"%@  %@阅读",times,_model.readnum];
    self.titleLab.text = _model.title;
    self.newsType.image = [UIImage imageNamed:[NSString stringWithFormat:@"%ld_headline",[[_model.code3 substringWithRange:NSMakeRange(3, 1)] integerValue] + 1]];
    self.addressLab.text = _model.aread;
    self.contentLab.text = _model.content;
    
    UIView *bottomView = self.contentLab;
    
    _photosContainer.photoNamesArray = [Tools leapImgAry:[Tools toJSONData:_model.upimg]];
    if (_photosContainer.photoNamesArray.count > 0) {
        _photosContainer.hidden = NO;
        bottomView = _photosContainer;
    } else {
        _photosContainer.hidden = YES;
    }
    
    [self setupAutoHeightWithBottomView:bottomView bottomMargin:10];
}

- (void)set_UI
{
    PhotosContainerView *photosContainer = [[PhotosContainerView alloc] initWithMaxItemsCount:9];
    _photosContainer = photosContainer;
    
    self.iconImg = [UIImageView new];
    self.newsType = [UIImageView new];
    LRViewRadius(self.iconImg, 20, 0.5, [UIColor clearColor]);
    
    self.nameLab = [[Tools shareInstance] creat_Label:@"" Color:K_TitlesColor Font:15 tag:-1 Alignment:0 Lines:1];
    self.timeLab = [[Tools shareInstance] creat_Label:@"" Color:[Util colorWithHexString:@"#999999"] Font:11 tag:-1 Alignment:0 Lines:1];
    self.titleLab = [[Tools shareInstance] creat_Label:@"" Color:[Util colorWithHexString:@"#27303e"] Font:16 tag:-1 Alignment:0 Lines:0];
    self.addressLab = [[Tools shareInstance] creat_Label:@"" Color:[Util colorWithHexString:@"#999999"] Font:11 tag:-1 Alignment:0 Lines:1];
    self.contentLab = [[Tools shareInstance] creat_Label:@"" Color:[Util colorWithHexString:@"#666666"] Font:14 tag:-1 Alignment:0 Lines:0];
    [self sd_addSubviews:@[self.iconImg,self.nameLab,self.timeLab,self.titleLab,self.newsType,self.addressLab,self.contentLab,photosContainer]];
    
    CGFloat left = 15;
    CGFloat top = 10;
    
    self.iconImg.sd_layout
    .leftSpaceToView(self, left)
    .topSpaceToView(self, top)
    .widthIs(40)
    .heightIs(40);
    
    self.nameLab.sd_layout
    .leftSpaceToView(self.iconImg, 10)
    .topEqualToView(self.iconImg)
    .widthIs(100)
    .heightIs(20);
    
    self.timeLab.sd_layout
    .leftEqualToView(self.nameLab)
    .topSpaceToView(self.nameLab, 6.5)
    .rightSpaceToView(self, left)
    .heightIs(11);
    
    self.titleLab.sd_layout
    .leftEqualToView(self.iconImg)
    .topSpaceToView(self.iconImg, 15)
    .rightSpaceToView(self, left)
    .autoHeightRatio(0);
    
    self.newsType.sd_layout
    .leftEqualToView(self.titleLab)
    .topSpaceToView(self.titleLab, 15.5)
    .widthIs(50)
    .heightIs(15);
    
    self.addressLab.sd_layout
    .leftSpaceToView(self.newsType, 11)
    .centerYEqualToView(self.newsType)
    .rightSpaceToView(self, left)
    .heightIs(11);
    
    self.contentLab.sd_layout
    .leftEqualToView(self.newsType)
    .topSpaceToView(self.newsType, 38)
    .rightSpaceToView(self, left)
    .autoHeightRatio(0);
    
    _photosContainer.sd_layout
    .leftEqualToView(self.contentLab)
    .topSpaceToView(self.contentLab,top)
    .rightEqualToView(self.contentLab);
    
}
@end
